package com.itheima.controller;

import com.itheima.domain.pojo.PageResult;
import com.itheima.domain.pojo.Student;
import com.itheima.domain.query.StudentQueryParam;
import com.itheima.domain.vo.Result;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Sch
 * @Date: 2025/07/14 19:32
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;


    /**
     * 学生列表查询
     */
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam) {
        log.info("查询参数：{}", studentQueryParam);
        PageResult pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 新增学生
     */
    @PostMapping
    public Result save(@RequestBody Student student) {
        log.info("新增学生：{}", student);
        studentService.save(student);
        return Result.success();
    }

    /**
     * 查询回显
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        log.info("根据id查询学生详细信息");
        return Result.success(studentService.getInfo(id));
    }

    /**
     * 修改学生信息
     */
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改学生");
        studentService.update(student);
        return Result.success();
    }
    /**
     * 删除学生
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable Integer[] ids) {
        log.info("删除学生");
        studentService.deleteById(ids);
        return Result.success();
    }

    /**
     * 违纪处理
     */
    @PutMapping("violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("处理违纪");
        Student student = studentService.getInfo(id);
        student.setViolationCount(student.getViolationCount() + 1);
        student.setViolationScore(student.getViolationScore() + score);
        studentService.update(student);
        return Result.success();
    }

}
